﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YTCMS.Core
{
    public class PagedList<T> : List<T>, IPagedList<T>
    {
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }
        public string Parameters { get; set; }
        public bool HasPreviousPage
        {
            get { return (PageIndex > 0); }
        }
        public bool HasNextPage
        {
            get { return (PageIndex + 1 < TotalPages); }
        }

        private int PageIndexVerify(int pageIndex)
        {
            if (pageIndex + 1 >= TotalPages)
            {
                if (TotalPages > 0)
                {
                    pageIndex = TotalPages - 1;
                }
                else
                {
                    pageIndex = TotalPages;
                }
            }
            return pageIndex;
        }
        /// <summary>
        /// 基本分页
        /// </summary>
        /// <param name="source"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        public PagedList(IEnumerable<T> source, int pageIndex, int pageSize)
        {
            this.Parameters = string.Empty;
            int total = source.Count();
            this.TotalCount = total;
            this.TotalPages = total / pageSize;

            if (total % pageSize > 0)
                TotalPages++;

            this.PageSize = pageSize;
            pageIndex = PageIndexVerify(pageIndex);
            this.PageIndex = pageIndex;
            this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
        }

        public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, string parametters)
        {
            this.Parameters = parametters;
            int total = source.Count();
            this.TotalCount = total;
            this.TotalPages = total / pageSize;
            if (total % pageSize > 0)
                TotalPages++;
            this.PageSize = pageSize;
            pageIndex = PageIndexVerify(pageIndex);
            this.PageIndex = pageIndex;
            this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
        }

        public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
        {
            this.Parameters = string.Empty;
            TotalCount = totalCount;
            TotalPages = TotalCount / pageSize;

            if (TotalCount % pageSize > 0)
                TotalPages++;

            this.PageSize = pageSize;
            pageIndex = PageIndexVerify(pageIndex);
            this.PageIndex = pageIndex;
            this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize));
        }

        public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount, string parametters, bool skip)
        {
            this.Parameters = parametters;
            TotalCount = totalCount;
            TotalPages = TotalCount / pageSize;

            if (TotalCount % pageSize > 0)
                TotalPages++;

            this.PageSize = pageSize;
            pageIndex = PageIndexVerify(pageIndex);
            this.PageIndex = pageIndex;
            this.AddRange(source);
        }

        public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, out int pcount)
        {
            this.Parameters = string.Empty;
            if (source != null)
            {
                int total = source.Count();
                this.TotalCount = total;
                this.TotalPages = total / pageSize;

                if (total % pageSize > 0)
                    TotalPages++;

                this.PageSize = pageSize;
                pageIndex = PageIndexVerify(pageIndex);
                this.PageIndex = pageIndex;
                this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
                if (source != null && source.Any())
                {
                    pcount = source.Count();
                }
                else
                {
                    pcount = 0;
                }
            }
            else
            {
                pcount = 0;
            }
        }

    }
}
